#ifndef SPACE_WAR_H
#define SPACE_WAR_H

#include <stdint.h>
#include <float.h>
#include <stdbool.h>
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <stdlib.h>
/*--------------------------response--------------------------*/
#define attack_max 15
#define clear_max 15
#define resource_max 90
#define line_max 500
#define column_max 100

#define state_width 1
#define state_line 3

#define point_width 5
#define point_line 7

#define unit_num_width 2
#define unit_num_line 11

#define pos_width 7
#define station_line_1  16
#define attack_line_1 20
#define clear_line_1 38

#define station_line_2  50
#define attack_line_2 54
#define clear_line_2 73

#define resource_line 84 

/*--------------------------command--------------------------*/
#define cmd_line_max 35
#define cmd_column_max 75

#define cmd_station_line 1
#define cmd_attack_line 4
#define cmd_clear_line 20

/*--------------------------tools--------------------------*/
#define min(type,x,y) ({\
    type _x = x;        \
    type _y = y;        \
    _x < _y ? _x : _y;  \
}) 

typedef struct position
{
    int32_t x;
    int32_t y;
    int r; //轨道
    double r_cur;//实际半径
    double theta;
} pos;

typedef struct resource_data
{
    int num;
    pos pos[resource_max];
} resource_data;


typedef struct player_state
{
    uint8_t station;           //0,1
    uint32_t point;         //0-99999
    uint8_t attack_number;  //0-15
    uint8_t clear_number;  //0-9
    pos station_pos;
    pos attack_units[attack_max];
    pos clear_units[clear_max];

} state;

typedef struct player_operation
{
    uint8_t station_ops;
    uint8_t station_move;
    uint8_t attack_ops[attack_max];
    uint8_t clear_ops[clear_max];
} operation;


//获取反馈
bool load_state(state *p1, state *p2, resource_data *resources, char *path, char flag);
//发送命令
void send_cmd(state p1,operation ops, char* path);
//判断反馈文件是否更新
bool is_refresh(char *path, pos origin);

int str2int(char * p);

pos line_to_position(char *line);

void line_to_point(char *line, state *p1, state *p2, char flag);

bool is_pos_equal(pos p1, pos p2);

void print_ops(operation ops);


//作为所有读入文件的接口，range为0-range行，为-1则读入全部
//成功则返回读取行数，失败返回-1
int load_file(char **lines, char *path, int range);

//命令行参数设置交互文件路径
void set_path(char *res_path, char *cmd_path, char flag);

bool send_file(char *path, char **lines, int num);

//根据反馈文件第一行判断游戏是否结束
//返回0：未结束，1：实际p1胜，2：实际p2胜
int is_end(char *path);
#endif
